package games.GluttonousSnake;

/**
 * Created by Administrator on 2016/12/10.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mytcs extends JFrame implements ActionListener,KeyListener{

    JButton Snakehead;                                                                  //定义蛇头
    JButton Snakebody[]=new JButton[500];                                               //定义蛇的身体  对象数组
    JButton food=new JButton();                                                             //定义食物
    JButton pauseButton=new JButton("暂停");
    int x,y,w,z;                                                                  //定义组件的横纵坐标
    Timer timeup,timedown,timeleft,timeright;                                  //相应的线程控制蛇头往相应的方向运动
    String direction="jk",direction2="jk";
    int keycounter=0,foodcounter=0,step=0,bodycounter=0;
    int Xtrack[]=new int[500],Ytrack[]=new int[500];                            //分别用于存放蛇头的x，y坐标
    JPanel panel;
    JMenuBar menubar;
    JMenu menu1,menu2;
    JMenuItem item1,item2,item3,item4,item5;
    JLabel label,label2,label3;
    Font font,font2;

    Mytcs(String s){
        setLayout(null);      //布局    绝对布局    所有的组件都用绝对的坐标表示
        setBounds(300,150,800,600);
        setVisible(true);      //设置是否可见
        setTitle(s);
        label=new JLabel("分数："+0);
        label2=new JLabel("按暂停后");
        label3=new JLabel("点击蛇头继续");
        label.setBounds(25,50,110,30);
        label2.setBounds(15,270,110,30);
        label3.setBounds(15,300,110,30);
        font=new Font("jk",Font.BOLD,25);
        font2=new Font("kj",Font.BOLD,15);
        label.setFont(font);
        label2.setFont(font2);
        label3.setFont(font2);
        panel=new JPanel();
        panel.setLayout(null);   //布局  panel容器
        add(panel);
        panel.add(label);
        panel.add(label2);
        panel.add(label3);
        panel.add(pauseButton);
        pauseButton.setBounds(25,225,90,30);
        panel.setBackground(Color.pink);
        panel.setBounds(650,0,150,600);

        menubar=new JMenuBar();
        menu1=new JMenu("游戏");
        menu2=new JMenu("难度");
        item1=new JMenuItem("小学生难度");
        item2=new JMenuItem("中等难度");
        item3=new JMenuItem("高手难度");
        item4=new JMenuItem("老司机难度");
        item5=new JMenuItem("开始新游戏");
        item1.addActionListener(this);          //添加事件
        item2.addActionListener(this);
        item3.addActionListener(this);
        item4.addActionListener(this);
        item5.addActionListener(this);
        pauseButton.addActionListener(this);
        menu1.add(item5);
        menu2.add(item1);  menu2.add(item2);
        menu2.add(item3);  menu2.add(item4);
        menubar.add(menu1);
        menubar.add(menu2);
        setJMenuBar(menubar);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //设置关闭

        food=new JButton();
        add(food);                                                                   //添加食物
        w=(int)(Math.random()*640/10)*10;
        z=(int)(Math.random()*530/10)*10;
        food.setBounds(w,z,10,10);
        food.setBackground(Color.YELLOW);


        Snakehead=new JButton();
        add(Snakehead);                                                             //添加蛇头
        x=(int)(Math.random()*640/10)*10;
        y=(int)(Math.random()*530/10)*10;
        Snakehead.addKeyListener(this);
        Snakehead.setBounds(x,y,10,10);
        Snakehead.setBackground(Color.RED);

        this.runGame();                                                              //开始游戏
    }

    public void runGame(){                                      //游戏运行的主方法
        timeup=new Timer(50,this);                                                   //每50ms运动一次
        timedown=new Timer(50,this);
        timeleft=new Timer(50,this);
        timeright=new Timer(50,this);

        while(true){

            if(direction.equals(direction2)==false){                              //当direction发上改变时，启动相应线程，改变蛇头的运动方向
                if(direction=="up"){
                    timeup.start();
                    direction2=direction;
                }
                else if(direction=="down"){
                    timedown.start();
                    direction2=direction;
                }
                else if(direction=="left"){
                    timeleft.start();
                    direction2=direction;

                }
                else if(direction=="right") {
                    timeright.start();
                    direction2=direction;
                }
            }

            if(w==x&&z==y){                                                    //当蛇头与食物重合时
                w=(int)(Math.random()*640/10)*10;
                z=(int)(Math.random()*530/10)*10;
                food.setBounds(w,z,10,10);                                  //食物重新随机出现在一个位置
                foodcounter++;
                this.GetBody(bodycounter);                                      //生成蛇的身体
                label.setText("分数:"+bodycounter);
            }
        }
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_UP){                     //当键盘输入一个方向后，关闭上一个线程，将direction赋值为相应的方向
            this.timeClose(direction,keycounter);
            if(direction=="up"){                               //当输入的方向与当前方向相同，就再次启动相应的线程。
                timeup.start();
            }
            direction="up";
            keycounter=keycounter+1;                           //键盘计数器+1
        }
        else if(e.getKeyCode()==KeyEvent.VK_DOWN){
            this.timeClose(direction,keycounter);
            if(direction=="down"){
                timedown.start();
            }
            direction="down";
            keycounter=keycounter+1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_LEFT){
            this.timeClose(direction,keycounter);
            if(direction=="left"){
                timeleft.start();
            }
            direction="left";
            keycounter=keycounter+1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            this.timeClose(direction,keycounter);
            if(direction=="right"){
                timeright.start();
            }
            direction="right";
            keycounter=keycounter+1;
        }
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==item1){
            timeup.setDelay(100);
            timedown.setDelay(100);
            timeleft.setDelay(100);
            timeright.setDelay(100);
        }
        else if(e.getSource()==item2){
            timeup.setDelay(50);
            timedown.setDelay(50);
            timeleft.setDelay(50);
            timeright.setDelay(50);
        }
        else if(e.getSource()==item3){
            timeup.setDelay(30);
            timedown.setDelay(30);
            timeleft.setDelay(30);
            timeright.setDelay(30);
        }
        else if(e.getSource()==item4){
            timeup.setDelay(10);
            timedown.setDelay(10);
            timeleft.setDelay(10);
            timeright.setDelay(10);
        }
        else if(e.getSource()==item5){
            this.NewGame();
        }
        if(e.getSource()==timeup){                               //对应的线程调用对应方法
            y=y-10;
            Snakehead.setBounds(x,y,10,10);
            this.RecordStep(step);                                //记录蛇头的轨迹
        }
        else if(e.getSource()==timedown){
            y=y+10;
            Snakehead.setBounds(x,y,10,10);
            this.RecordStep(step);
        }
        else if(e.getSource()==timeleft){
            x=x-10;
            Snakehead.setBounds(x,y,10,10);
            this.RecordStep(step);
        }
        else if(e.getSource()==timeright){
            x=x+10;
            Snakehead.setBounds(x,y,10,10);
            this.RecordStep(step);
        }
        if(e.getSource()==pauseButton){
            this.timeClose(direction, keycounter);
        }
    }

    public void keyReleased(KeyEvent e){}

    public void keyTyped(KeyEvent e){}

    public void timeClose(String s,int counter){          //当不是第一次按动方向键时，即已经蛇开始运动，就关闭相应线程
        if (counter!=0){
            if(s=="up"){
                timeup.stop();
            }
            else if(s=="down"){
                timedown.stop();
            }
            else if(s=="left"){
                timeleft.stop();
            }
            else if(s=="right"){
                timeright.stop();
            }
        }
    }

    public void RecordStep(int i){                              //记录蛇头的轨迹
        if(i==0){
            Xtrack[i]=x; Ytrack[i]=y;
            step++;
        }
        else if(i>0&&i<500){
            for(int a=i;a>0;a--){
                Xtrack[a]=Xtrack[a-1];
                Ytrack[a]=Ytrack[a-1];
            }
            Xtrack[0]=x;                                            //蛇头当前的位置存放在(Xtrack[0]，Ytrack[0])
            Ytrack[0]=y;
            step++;
        }
        else{
            for(int a=499;a>0;a--){
                Xtrack[a]=Xtrack[a-1];
                Ytrack[a]=Ytrack[a-1];
            }
            Xtrack[0]=x;
            Ytrack[0]=y;
            step++;
        }
        this.BodyMove(bodycounter);                            //蛇头的位置每变化一次，就调用方法使身体随之移动
        this.gameOver();                                          //蛇头的位置每变化一次，就判断游戏是否结束
    }

    public void GetBody(int i){
        Snakebody[i]=new JButton();
        add(Snakebody[bodycounter]);
        Snakebody[i].setBounds(Xtrack[i+1],Ytrack[i+1],10,10);
        bodycounter++;                                                      //身体计数器+1
    }

    public void BodyMove(int i){                                      //使蛇身体跟随蛇头的轨迹运动
        if(i>0){
            for(int a=0;a<i;a++){
                Snakebody[a].setBounds(Xtrack[a+1],Ytrack[a+1],10,10);
            }
        }
    }

    public void gameOver(){                                                //判断游戏是否结束，结束则开启新游戏
        if(x>640||y>530||x<0||y<0) {
            this.timeClose(direction, keycounter);
            JOptionPane.showMessageDialog(this, "蛇撞到了墙，你弱爆了");
            this.NewGame();                                                 //启动新游戏
        }
        for(int i=1;i<bodycounter;i++){
            if(x==Xtrack[i]&&y==Ytrack[i]){
                this.timeClose(direction, keycounter);
                JOptionPane.showMessageDialog(this, "这都能咬到自己，智障吗？");
                this.NewGame();                                                      //启动新游戏
                break;
            }
        }
    }

    public void NewGame(){                                             //启动新游戏的方法
        keycounter=0;
        foodcounter=0;
        step=0;
        for(int i=0;i<bodycounter;i++){
            remove( Snakebody[i]);
        }
        bodycounter=0;
        direction="jk";
        direction2="jk";
        label.setText("分数"+0);
        x=(int)(Math.random()*640/10)*10;
        y=(int)(Math.random()*530/10)*10;
        w=(int)(Math.random()*640/10)*10;
        z=(int)(Math.random()*530/10)*10;
        Snakehead.setBounds(x,y,10,10);
        food.setBounds(w,z,10,10);
        repaint();
    }
}
